package com.shuaitao.newsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuaitao.newsystem.bean.News;
import com.shuaitao.newsystem.bean.Topic;
import com.shuaitao.newsystem.service.NewsService;
import com.shuaitao.newsystem.service.TopicService;
import com.shuaitao.newsystem.utils.R;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.net.idn.Punycode;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Controller
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    private TopicService topicService;
    @Autowired
    private NewsService newsService;

    @RequestMapping("/getAllTopic")
    public void getAllTopic(HttpSession session) {
        List<Topic> list = topicService.list();
        session.setAttribute("topic",list);
    }
    @RequestMapping("toTopicAdd")
    public String toTopicAdd(HttpSession session) {
        session.removeAttribute("topicDetail");
        return "newspages/topic_add";
    }
    @RequestMapping("toTopicEdit")
    public String toTopicEdit(HttpSession session) {
        List<Topic> list = topicService.list();
        session.setAttribute("topic",list);
//        session.removeAttribute("topicDetail");
        return "newspages/topic_modify";
    }
    @PostMapping("addTopic")
    @ResponseBody
    public R addTopic(@NotBlank @RequestParam("tname")String tname) {
        Topic topic = new Topic();
        topic.setTname(tname);
        Topic tname1 = topicService.getOne(new QueryWrapper<Topic>().eq("tname", tname));
        if (tname1 != null) {
            return R.error(101,"topic重复！！！");
        }
        boolean save = topicService.save(topic);
        if (save) {
            return R.ok();
        }
        return R.error(102,"出意外了！！！");
    }
    @PostMapping("editTopic")
    @ResponseBody
    public R editTopic(@NotBlank @RequestParam("tname")String tname,@RequestParam("tid") Integer tid) {
        Topic topic = new Topic();
        topic.setTname(tname);
        topic.setTid(tid);
        int tname1 = topicService.count(new QueryWrapper<Topic>().eq("tname", tname));
        if (tname1 >=1 ) {
            return R.error(101,"topic重复！！！");
        }
        boolean b = topicService.updateById(topic);
        if (b) {
            return R.ok();
        }
        return R.error(102,"出错了");
    }
    @GetMapping("toTopicEditDetail")
    public String toTopicEditDetail(@RequestParam("tid") Integer tid,HttpSession session) {
        Topic topic = topicService.getOne(new QueryWrapper<Topic>().eq("tid", tid));
        session.setAttribute("topicDetail",topic);
        return "newspages/topic_add";

    }
    @GetMapping("delTopic")
    @ResponseBody
    public R deTopic(@RequestParam("tid") Integer tid) {
        int ntid = newsService.count(new QueryWrapper<News>().eq("ntid", tid));
        if(ntid > 0) {
            return R.error(102,"删除失败，当前topic下有未删除的news");
        }
        boolean b = topicService.removeById(tid);
        if (b) {
            return R.ok();
        }
        return R.error(101,"出意外了！！！");

    }
}
